package com.jinsi.controller.tang;

import com.jinsi.constant.TangConstant;
import com.jinsi.pojo.SyUser;
import com.jinsi.service.tang.JournalService;
import com.jinsi.service.tang.ProductAddService;
import com.jinsi.util.R;
import com.jinsi.util.Sensitive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 添加商品列表
 * @author 唐文博
 * @Date 2022-08-05
 */
@RestController
@RequestMapping("/add")
@Slf4j
public class ProductAddController {
    @Autowired
    private ProductAddService productAddService;

    @Autowired
    private JournalService journalService;

    @Autowired
    private Sensitive sensitive;

    /**
     * 添加接口
     * @param commodityCode
     * @param classifyId
     * @param commodityName
     * @param searchKeywords
     * @param commodityImage
     * @param currentPrice
     * @param commodityState
     * @param commodityLabel
     * @param recommend
     * @param session
     * @return
     */
    @RequestMapping("/productAdd")
    public R productAdd(int commodityCode, int classifyId, String commodityName, String searchKeywords,
                        String commodityImage, int currentPrice, int commodityState, String commodityLabel,
                        int recommend, HttpSession session,String body) {
        Date date = new Date();
        SyUser user = (SyUser) session.getAttribute("user");
        // 过滤敏感词
        Map<String, Object> map = sensitive.getSensitiveCharacter(body);
        int stateR = (int) map.get("state");
        String causeR = (String) map.get("cause");
        String bodyR = (String) map.get("body");

        Sensitive sensitive = new Sensitive();
        try {
            log.info("添加商品: [开始] ,用户id：{}",session);
            productAddService.addCommodity(commodityCode,classifyId,commodityName,searchKeywords,
                    commodityImage, currentPrice,commodityState,commodityLabel, recommend,
                    TangConstant.COMMODITY_ISDEL,user.getUserId(),date, TangConstant.EXAMINE_REVIEWED,
                    TangConstant.EXAMINE_DISPLAY);
            log.info("添加商品记录日志: [开始] 结果：{}","开始");
            journalService.addCkJournal(user.getUserName(),TangConstant.PRODUCT_LIST,
                    date,TangConstant.PRODUCT_ADD,TangConstant.PRODUCT_PRODUCTADD,
                    TangConstant.COMMODITY_ISDEL);
            log.info("添加商品记录日志: [结束] 结果：{}","成功");
            log.info("添加商品: [结束] 结果：{}","成功");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加商品: [失败]", e);
            return R.error();
        }
    }

    /**
     * 修改假删除
     * @param commodityId
     * @param session
     * @return
     */
    @RequestMapping("/productDelete")
    public R productDelete(Integer commodityId, HttpSession session) {
        Date date = new Date();
        SyUser user = (SyUser) session.getAttribute("user");
        try {
            log.info("删除商品: [开始], 商品信息:{},用户id：{}",commodityId,session);
            productAddService.deleteCommodity(commodityId);
            log.info("删除商品记录日志: [开始] 结果：{}","开始");
            journalService.addCkJournal(user.getUserName(),TangConstant.PRODUCT_LIST,
                    date,TangConstant.PRODUCT_DELETE,TangConstant.PRODUCT_PRODUCTDELETE,
                    TangConstant.COMMODITY_ISDEL);
            log.info("删除商品记录日志: [结束] 结果：{}","成功");
            log.info("删除商品: [结束] 结果：{}","成功");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除商品: [失败]", e);
            return R.error();
        }
    }

}
